﻿<%@ WebHandler Language="VB" Class="UploadAlbum" %>

Imports System
Imports System.Web
Imports System.Data
Imports VSModel
Imports Newtonsoft.Json
Imports System.Xml
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging

Public Class UploadAlbum : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim fileCol As HttpFileCollection = context.Request.Files
        '  Dim rootPath As String = ConfigurationManager.AppSettings(context.Request.QueryString("folder"))
        Dim rootPath As String = context.Server.MapPath("~/Uploads/")
        'Logic to find the FileName (excluding the path)
        Dim DestPath = rootPath & "\" & Now.Year.ToString.Trim() & "\" & Now.Month.ToString.Trim() & "-" & Now.Day.ToString.Trim()
        Dim StringDate = Now.Year.ToString.Trim() & "/" & Now.Month.ToString.Trim() & "-" & Now.Day.ToString.Trim()
        'create thu mutc theo year + month
        If Not Directory.Exists(DestPath) Then
            Directory.CreateDirectory(DestPath)
        End If
        Dim arr As String = ""
        For i As Integer = 0 To fileCol.Count - 1
            Dim file As HttpPostedFile = fileCol(i)
            Dim intFileNameLength As Integer
            Dim strFileName As String
            Dim random As New Random
          
            'Logic to find the FileName (excluding the path)
            strFileName = file.FileName
            If (System.IO.File.Exists(DestPath & "/" & strFileName)) Then
                intFileNameLength = InStr(1, StrReverse(strFileName), "\")
                strFileName = (1 + random.NextDouble).ToString() + strFileName
                context.Request.Files("fileAlbum").SaveAs(DestPath & "/" & strFileName)
                arr += StringDate & "/" & strFileName + ","
            Else
                context.Request.Files("fileAlbum").SaveAs(DestPath & "/" & strFileName)
                arr += StringDate & "/" & strFileName + ","
            End If
            'check if name contain image type
            Dim ImageType As String() = {"JPG", "GIF", "PNG", "JPEG", "BMP"}
            Dim ext As String = strFileName.Substring(strFileName.LastIndexOf(".") + 1, strFileName.Length - strFileName.LastIndexOf(".") - 1)
            Dim check As Boolean = IIf(ImageType.Contains(ext.ToUpper()), True, False)
            ' Check isImage {705, 1245}
            If check Then
                Dim imgWidth As Integer
                Dim imgHeight As Integer
                Dim strNewFile As String
                Dim path As String = ""
                Dim thumbPath As String
                Dim arrImageHot(,) As Integer = {{48, 68}, {400, 564}, {1200, 1200}}
                
                For j As Integer = 0 To UBound(arrImageHot)
                    imgWidth = arrImageHot(j, 0)
                    imgHeight = arrImageHot(j, 1)
                    path = DestPath & "/" & strFileName
                    strNewFile = imgWidth.ToString & "-" & imgHeight & "-" & strFileName
                    thumbPath = DestPath & "/" & strNewFile
            
                    Dim sourceFile = System.IO.File.OpenRead(path)
                    Dim desFile = System.IO.File.Create(thumbPath)
                    ResizeImage(imgWidth, imgHeight, sourceFile, desFile)
            
                    sourceFile.Close()
                    desFile.Close()
                Next
            End If
        Next
        context.Response.Write(arr)
    End Sub
    Protected Sub ResizeImage(ByVal width As Integer, ByVal height As Integer, ByVal fromStream As Stream, ByVal toStream As Stream)
        'just thumb by with ignor height
        Dim image As Image = image.FromStream(fromStream)
        'image = CreateWatermark(image, "OtoSaiThanh.com")
        Dim newWidth = width
        Dim newHeight = CInt(image.Height * (width / image.Width))
        Dim thumbnailBitmap = New Bitmap(newWidth, newHeight)
 
        Dim thumbnailGraph = Graphics.FromImage(thumbnailBitmap)
        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality
        thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality
        thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic
        
        Dim imageRectangle = New Rectangle(0, 0, newWidth, newHeight)
        
        thumbnailGraph.DrawImage(image, imageRectangle)
        Dim strWatermark = "lolitastorevn.com"
        
        Dim xDraw = (newWidth) 
        Dim yDraw = (newHeight)
        
        
        If width = 48 Then
            'thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 6, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 0, 0, 0)), xDraw + 2, yDraw + 2)
            'thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 6, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 255, 255, 255)), xDraw, yDraw)
        ElseIf width = 400 Then
            
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 0, 0, 0)), xDraw - 132, yDraw - 18)
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 10, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 255, 255, 255)), xDraw - 130, yDraw - 20)
        ElseIf width = 624 Then
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 14, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 0, 0, 0)), xDraw - 198, yDraw - 28)
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 14, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 255, 255, 255)), xDraw - 200, yDraw - 30)
        Else
                        
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 12, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 0, 0, 0)), xDraw - 160, yDraw - 28)
            thumbnailGraph.DrawString(strWatermark, New Font("Verdana", 12, FontStyle.Bold), New SolidBrush(Color.FromArgb(128, 255, 255, 255)), xDraw - 158, yDraw - 30)

            
        End If
        
        
        thumbnailBitmap.Save(toStream, image.RawFormat)
        thumbnailGraph.Dispose()
        thumbnailBitmap.Dispose()
        image.Dispose()
    End Sub
        
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class